<template>
  <div class="login">
    <loginHead />
    <div class="options">
      <span>
        <router-link to="/login">手机号</router-link>
      </span>
      <span>
        <router-link to="/studentLogin">学号</router-link>
      </span>
      <span>
        <router-link class="show" to="/logup">注册</router-link>
      </span>
    </div>
    <div class="hr"></div>
    <ul class="info">
      <li>
        <i class="school"></i>
        <input type="text" placeholder="请输入手机号" v-model="text" />
      </li>
      <li>
        <i class="sid"></i>
        <input type="password" placeholder="请输入密码" v-model="password" />
      </li>
      <li>
        <i class="sid"></i>
        <input type="password" placeholder="请输入确认密码" v-model="sms" />
      </li>
      <button class="btn" @click="logup">注册</button>
    </ul>
    <div class="foot">
      <div class="rem">
        点击“注册”按钮，即代表您同意<a>《智慧树用户协议》</a>、<a
          >《隐私政策》</a
        >
      </div>
      <div class="footer">如登录、注册遇到问题，请 <a>联系客服</a></div>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
export default {
  data() {
    return {
      text: "",
      password: "",
      sms: "",
    };
  },
  methods: {
    logup() {
      let regphone = /0?(13|14|15|17|18|19)[0-9]{9}/;
      let regpassword = /^\d{6}$/;
      if (!this.text || !this.password || !this.sms) {
        Toast("手机号或密码或确认密码不能为空");
        return;
      }
      if (!regphone.test(this.text)) {
        Toast("请输入正确格式的手机号");
        return;
      }
      if (!regpassword.test(this.password)) {
        Toast("请输入正确格式的6位数字密码");
        return;
      }
      if (this.sms != this.password) {
        Toast("确认密码和密码不一致");
        return;
      }
      this.$http
        .post("http://test.com:3000/user/logup", {
          username: this.text,
          password: this.password,
        })
        .then((res) => {
          console.log(res);
          if (res.data.code == 200) {
            Toast(res.data.msg);
            //把username存起来（用于验证是否已登录）
            sessionStorage.setItem("userData", JSON.stringify(res.data.user));
            // console.log(res.data.user);
            setTimeout(() => {
              window.location.href = "/";
            }, 1000);
          } else {
            Toast(res.data.msg);
          }
        });
    },
  },
};
</script>

<style lang="less" scoped>
@height: 10vh;
@border: 1px solid;
.login {
  position: absolute;
  left: 50%;
  top: 50%;
  margin-top: -315px;
  margin-left: -200px;
  width: 400px;
  height: 630px;
  background: #ffffff;
  box-shadow: 0 0 10px 0 #e6e6e6;
  border-radius: 6px;
  .options {
    position: relative;
    width: 355px;
    height: 40px;
    text-align: center;
    margin: 0 auto;
    margin-top: 40px;
    a {
      display: inline-block;
      width: 60px;
      height: 35px;
      line-height: 25px;
      font-size: 14px;
      color: #777;
      transition: all ease-in 0.2s;
      margin-right: 20px;
      text-align: center;
    }
    .show {
      font-weight: bold;
      font-size: 16px;
      color: #2a2a2a;
      border-bottom: 5px solid #3d84ff;
    }
  }
  .hr {
    margin-top: 5px;
    border-bottom: 1px solid #e6e6e6;
  }
  .info {
    width: 85%;
    margin: 0 auto;
    li {
      position: relative;
      height: 40px;
      width: 340px;
      background: #f6f6f6;
      border: 1px solid #ededed;
      margin-top: 20px;
      i {
        width: 18px;
        height: 20px;
        z-index: 9;
      }
      .school {
        background: url("../../assets/images/icon.png") 0 0 no-repeat;
        position: absolute;
        left: 13px;
        top: 11px;
      }
      .sid {
        background: url("../../assets/images/icon.png") -88px 0 no-repeat;
        position: absolute;
        left: 13px;
        top: 11px;
      }
      .passwd {
        background: url("../../assets/images/icon.png") -148px 0 no-repeat;
        position: absolute;
        left: 13px;
        top: 11px;
      }
      input {
        width: 80%;
        height: 40px;
        outline: none;
        border: none;
        margin-left: 10px;
        padding-left: 5px;
        border-left: 1px solid rgba(119, 119, 119, 0.133);
        background-color: #f6f6f6;
      }
    }
    .btn {
      width: 340px;
      height: 40px;
      line-height: 40px;
      font-size: 16px;
      color: #fff;
      text-align: center;
      cursor: pointer;
      background: #3d84ff;
      border: none;
      margin: 42px auto 0;
    }
  }
  .foot {
    // position: relative;
    width: 85%;
    margin: 0 auto;
    margin-top: 10px;
    .footer {
      position: absolute;
      width: 340px;
      bottom: 20px;
      font-size: 12px;
      color: #bfbfbf;
      a {
        font-size: 12px;
        color: #000;
        cursor: pointer;
      }
    }
    .rem {
      font-size: 12px;
      a {
        font-size: 12px;
        color: #3d84ff;
      }
    }
  }
}
</style>